<template>
  <el-drawer
    v-model="drawerVisible"
    title="添加房租信息"
    direction="rtl"
    size="40%"
  >
    <div class="rent-form-content">
      <el-form :model="formData" :rules="formRules" ref="formRef" label-width="100px">
        <el-form-item label="房租名称" prop="rentName">
          <el-input v-model="formData.rentName" placeholder="请输入房租名称" />
        </el-form-item>
        <el-form-item label="面积" prop="area">
          <el-input-number v-model="formData.area" :min="0" :max="99999" placeholder="请输入面积" style="width: 40" />
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="formData.location" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker v-model="formData.startDate" type="date" placeholder="选择开始日期" style="width: 100%" />
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker v-model="formData.endDate" type="date" placeholder="选择结束日期" style="width: 100%" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">保存</el-button>
          <el-button @click="drawerVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-drawer>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['submit'])

const drawerVisible = ref(false)
const formRef = ref()
const formData = reactive({
  rentName: '',
  area: '',
  location: '',
  startDate: '',
  endDate: ''
})

const formRules = {
  rentName: [{ required: true, message: '请输入房租名称', trigger: 'blur' }],
  area: [{ required: true, message: '请输入面积', trigger: 'blur' }],
  location: [{ required: true, message: '请输入位置', trigger: 'blur' }],
  startDate: [{ required: true, message: '请选择开始日期', trigger: 'change' }],
  endDate: [{ required: true, message: '请选择结束日期', trigger: 'change' }]
}

const openDrawer = (groupInfo?: any) => {
  drawerVisible.value = true
  Object.assign(formData, {
    rentName: '',
    area: '',
    location: '',
    startDate: '',
    endDate: ''
  })
}

const handleSubmit = () => {
  formRef.value.validate().then(() => {
    emit('submit', { ...formData })
    drawerVisible.value = false
    ElMessage.success('添加成功！')
  })
}

defineExpose({
  openDrawer,
  drawerVisible
})
</script>

<style lang="scss" scoped>
.rent-form-content {
  padding: 0 16px;
}
</style>
